﻿// JScript 文件
document.write("<script src=\"http://f.yaolanimage.cn/cms/image/js/md5.js\" type=\"text/javascript\"></script>");

if(typeof jQuery == 'undefined'){
   document.write('<script src="http://g.yaolanimage.cn/assets/js/jquery.min.js" type="text/javascript"></script>');
} 
//document.domain="yaolan.com";


String.prototype.trim = function(){
	
	return this.replace( /(^\s*)|(\s*$)/g, '' ) ;
};

//验证码
function refreshCc() 
{
    var ccImg = document.getElementById("checkCodeImg");
    var ccImgSrc = "";
    if (ccImg) {
	    nowTime = new Date();
	    ccImgSrc = "http://loginbar.yaolan.com/CheckCode.aspx?r=" + nowTime.getTime();
	    ccImg.src = ccImgSrc;
    }
}

function closeEditor()
{  
    var layer = window.parent.document.getElementById('layer');
    var widgetEditor = window.parent.document.getElementById('WidgetEditor');
    if(layer != null) window.parent.document.body.removeChild(layer);   
    if(widgetEditor != null ) window.parent.document.body.removeChild(widgetEditor); 
    //window.parent.document.body.style.position = '';   
}


//退出
function CancelLogin() 
{
    closeEditor();
}

function checkKey(e, eventsource)
{			
    var key = window.event ? e.keyCode : e.which;
    if (key.toString() == "13") {
        if (window.event) {
            e.returnValue = false;
            e.cancelBubble = true;
        }
        else {
            e.stopPropagation();
            e.preventDefault();
        }
        eventsource.click();			      
    }
}
function loginUser(){
    var userName = document.getElementById("txtUserName").value.trim();
    
    if(userName == ""){
        alert("用户名不能为空！");
        document.getElementById("txtUserName").focus();
       return false;
    }
    if(document.getElementById("txtPwd").value.trim() == ""){
        alert("用户密码不能为空！");
        document.getElementById("txtPwd").focus();
        return false;
    }
    var checkCode= document.getElementById("txtCheckCode");
    if(checkCode != null && checkCode.value.trim() == ""){
        alert("请输入验证码！");
        document.getElementById("txtCheckCode").focus();
        return false;
    }
    return true;
}

//提交数据
function SubmitData(pwd){
	document.domain = "yaolan.com";        
    var form = document.createElement('form');
    form.id="the_form";        
    form.method='post';
    form.name="the_form_";
    form.action="http://user.yaolan.com/ClientLogin.aspx"; 
	
	if(document.getElementById("inner_login")){
		form.target= "inner_login";
	}
   
    //用户名
    var inputName=document.createElement("input");
    inputName.id="name";
    inputName.name="name";
    inputName.type="hidden";
    inputName.value = document.getElementById("txtUserName").value;
    form.appendChild(inputName);

    //密码
    var inputPsw=document.createElement("input");
    inputPsw.id="pwd";
    inputPsw.name="pwd";
    inputPsw.type="hidden";
    var pwdMd5 = md5(pwd, 8, 16); 
    pwdMd5 = md5(inputName.value.trim() + pwdMd5, 16, 32);
    inputPsw.value = pwdMd5;            
    form.appendChild(inputPsw);
    
    //是否记住Cookies
    var is_remember = document.getElementById("chkRemember").checked;
    var rememberCookies=document.createElement("input");
    rememberCookies.id="is_remember";
    rememberCookies.name="is_remember";
    rememberCookies.type="hidden";
    rememberCookies.value = is_remember;
    form.appendChild(rememberCookies);       
    
    //成功跳转页面
    var inputSucc=document.createElement("input");
    inputSucc.id="succ_url";
    inputSucc.name="succ_url";
    inputSucc.type="hidden";
    inputSucc.value = "http://loginbar.yaolan.com/global/success.shtml";
    form.appendChild(inputSucc);
    
    var inputBack = document.createElement("input");
    inputBack.id = "active_succ_url";
    inputBack.name = "active_succ_url";
    inputBack.type = "hidden";
    inputBack.value = top.window.location.href;
    form.appendChild(inputBack);
    
    //失败跳转页面
    var inputFail=document.createElement("input");
    inputFail.id="fail_url";
    inputFail.name="fail_url";
    inputFail.type="hidden";
    inputFail.value = document.getElementById("txtFail_Url").value;
    form.appendChild(inputFail);        
   
    document.body.appendChild(form);          
    form.submit();
    document.body.removeChild(form);
    
    //load...
	if(document.getElementById("divLogin")){
     document.getElementById("divLogin").style.display = "none";
	 }
	 if(document.getElementById("divLoad")){
     document.getElementById("divLoad").style.display = "block";
	 }
     
}
//一下函数是首页登录
function Main_InputCheck()
{
    var userName = document.getElementById("txtUserName").value.trim();
    if(userName == "")
    {
        alert("用户名不能为空！");
        document.getElementById("txtUserName").focus();
        return;
    }
    if(document.getElementById("txtPwd").value.trim() == "")
    {
        alert("用户密码不能为空！");
        document.getElementById("txtPwd").focus();
        return;
    }
    CloseLoginDiv();
    Main_Login();
}
function Main_Blur(inputType)
{
    if(inputType=="name")
    {
        var userObj = document.getElementById("txtUserName");
        if(userObj.value.trim() == "")
        {
            userObj.style.backgroundImage = 'url(/images/login_btn.png)';
        }
    }
    else
    {
        var pwdObj = document.getElementById("txtPwd");
        if(pwdObj.value.trim() == "")
        {
            pwdObj.style.backgroundImage = 'url(/images/login_btn.png)';
        }
    }
}
//登录操作
function Main_Login(){
	     
    var strUserName = document.getElementById("txtUserName").value.trim();
    var pwd = document.getElementById("txtPwd").value.trim(); 
    var form = document.createElement('form');
    
    //登录
    form.id="the_form";        
    form.method='post';
    form.name="the_form_";
    form.action="http://user.yaolan.com/ClientLogin.aspx";
	
	if(document.getElementById("inner_login")){
		form.target= "inner_login";
	}
	
    //用户名
    var inputName=document.createElement("input");
    inputName.id="name";
    inputName.name="name";
    inputName.type="hidden";
    inputName.value = strUserName;
    form.appendChild(inputName);
    //密码    
    var inputPsw=document.createElement("input");
    inputPsw.id="pwd";
    inputPsw.name="pwd";
    inputPsw.type="hidden";
    var pwdMd5 = md5(pwd, 8, 16); 
    pwdMd5 = md5(inputName.value.trim() + pwdMd5, 16, 32);
    inputPsw.value = pwdMd5;            
    form.appendChild(inputPsw);
    //是否记住Cookies
    var is_remember = document.getElementById("chkRemember").checked; //首页登录默认记住密码
    var rememberCookies=document.createElement("input");
    rememberCookies.id="is_remember";
    rememberCookies.name="is_remember";
    rememberCookies.type="hidden";
    rememberCookies.value = is_remember;
    form.appendChild(rememberCookies);
    //成功跳转页面
    var inputSucc=document.createElement("input");    
    inputSucc.id="succ_url";
    inputSucc.name="succ_url";
    inputSucc.type="hidden";
    inputSucc.value = 'http://loginbar.yaolan.com/IFrame/IIndexLogin.aspx?issucess=1';
    form.appendChild(inputSucc);    
    //失败跳转页面
    var inputFail=document.createElement("input");
    inputFail.id="fail_url";
    inputFail.name="fail_url";
    inputFail.type="hidden";
    inputFail.value = 'http://loginbar.yaolan.com/IFrame/IIndexLogin.aspx';
    form.appendChild(inputFail);
    //发请求
    document.body.appendChild(form);          
    form.submit();
    document.body.removeChild(form);
}


//新增登录代码 begin dsp 20130409
function createField(name, value) {
    var field = document.createElement("input");
    field.setAttribute("type", "hidden");
    field.setAttribute("name", name);
    field.setAttribute("value", value);
    return field;
}

function postTo(params) {
    var hform = document.createElement("FORM");
    hform.setAttribute("accept-charset", params.charset);
   
    hform.setAttribute("target", params.target);
    // console.info(params.target);
    hform.setAttribute("action", params.url);
    hform.setAttribute("method", params.method);   
    document.body.appendChild(hform);
    for (var i = 0; i < params.fields.length; i++) {
        var field = params.fields[i];
        hform.appendChild(createField(field.name, field.value));
    }
    hform.submit();
    hform.parentNode.removeChild(hform);
}
function hashPsswd() {
    var name = $yl('ylLoignUname');
    var psswd = $yl('ylLoignUpwd');

    var pwdMd5 = md5(psswd.value, 8, 16);
    var psswdMd5 = md5(name.value + pwdMd5, 16, 32);

    return psswdMd5;
}
var hdpwd = document.getElementById("ylLoignUpwd");
if (hdpwd) {
    hdpwd.onkeydown = function(e) {
        var e = window.event ? window.event : e;
        if (e.keyCode == 13) {
            if (ylLoginCheck()) { ylLoginSubmitData(); }
            return false;
        }
    };
}


function ylLoginSubmitData() {
    document.domain = "yaolan.com";
    var url = location.href;
    postTo({
        url: "http://user.yaolan.com/ClientLogin.aspx"
          , method: "post"
          , target: "inner_login"
          , charset: "utf-8"
          , fields: [{ name: "name", value: document.getElementById('ylLoignUname').value }
              , { name: "pwd", value: hashPsswd() }
              , { name: "succ_url", value: "http://loginbar.yaolan.com/global/success.shtml" }
              , { name: "fail_url", value: "http://loginbar.yaolan.com/global/fail.shtml" }
              , { name: "is_remember", value: document.getElementById("ylLoginCheckbox").checked }
              , { name: "skip_active_tip", value: "false" }
              , { name: "active_succ_url", value: url }
          ]
    });
}
function ipcClone(source) {
    var dest = {};
    for (var p in source) {
        dest[p] = source[p];
    }
    return dest;
}
function whenFail(params) {
    var cloneParams = ipcClone(params);
    var errorCode = cloneParams["err_code"];
    var errMsg = "";
    var sFistErrCode = errorCode;
    switch (sFistErrCode) {
        case '101':
            errMsg = "用户名为空！";
            break;
        case '102':
            errMsg = "用户名不存在！";
            break;
        case '103':
            errMsg = "用户名中含有非法字符！";
            break;
        case '201':
            errMsg = "密码为空！";
            break;
        case '202':
            errMsg = "密码错误！"; 
            break;
        default:
            sRetErrMsg = "发生未知错误！请稍后重试！";
            break;
    }

  
    if (errMsg != "") {
        $yl("ylLoginError").style.display = "block";
        $yl("ylLoginError").innerHTML = errMsg;
    }

    if (sFistErrCode == '101' || sFistErrCode == '102' || sFistErrCode == '103') {
        $yl("ylLoignUname").parentNode.className += " " + 'ylLoginTips';
        $yl("ylLoignUname").parentNode.focus();
    } else {
        removeClass($yl("ylLoignUname").parentNode, "ylLoginTips");
    }
    if (sFistErrCode =='201' || sFistErrCode == '202'){
        $yl("ylLoignUpwd").parentNode.className += " " + 'ylLoginTips';
        $yl("ylLoignUpwd").parentNode.focus();
    } else {
        removeClass($yl("ylLoignUpwd").parentNode, "ylLoginTips");
    }
    
}

function whenSuccess() {
    location.href = location.href;
}
function goConnector(fromWeb) {
    if (fromWeb == 'qq') {
        location.href = "http://user.yaolan.com/Connect/QQ/Login.aspx?back_url=" + top.location.href;
    }
    if (fromWeb == 'weibo') {
        location.href = "http://user.yaolan.com/Connect/SinaTweets/Login.aspx?back_url=" + top.location.href;
    }
    if (fromWeb == 'renren') {
        location.href = "https://graph.renren.com/oauth/authorize?client_id=c454868876824458837735f572c90c75&redirect_uri=http://user.yaolan.com/Connect/renren/index.aspx?back_url=" + location.href + "&response_type=code?burl=" + top.location.href;
    }
}
function $yl(Id) { return document.getElementById(Id); }
//新增登录代码 end dsp 20130409


















